//
//  ModifyPwdView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/30.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ModifyPwdView: UIView {
    
    // 常量
    private let titles = ["新密码","再次确认"]
    private let placeholders = ["请输入新密码","请再次确认密码"]
    // 存储
    private var holderViews = [TextFieldOffsetPlaceHolder]()
    
    // MARK: 回调
    public var complete:((_ item:CallbackBean) -> Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 记录最后一个view高度
        var tempHeight:CGFloat = 0
        (0..<titles.count).forEach { (i) in
            let labelY = MARGIN + CGFloat(i) * MARGIN70
            // 标题
            let label = UILabel(title: titles[i], color: COLOR_NICE, size: 17)
            label.frame = CGRect(x: MARGIN, y: labelY, width: MARGIN * 4.8, height: MARGIN30)
            addSubview(label)
            // 输入
            let input = holderViews[i]
            input.isSecureTextEntry = true
            if #available(iOS 12.0, *) {
                input.textContentType = .newPassword
            }
            if #available(iOS 10.0, *) {
                input.textContentType = .password
            }
            input.keyboardType = .asciiCapable
            input.frame = CGRect(x: MARGIN, y: labelY, width: SCREEN_WIDTH, height: MARGIN30)
            // 下划线
            let line = UIView()
            line.backgroundColor = COLOR_LINE
            line.frame = CGRect(x: MARGIN, y: input.frame.maxY + MARGIN, width: SCREEN_WIDTH - MARGIN * 2, height: 1)
            addSubview(line)
            // 最后一个view
            if i == titles.count - 1 {
                tempHeight = line.frame.maxY
            }
        }
        // 确认修改按钮
        let saveBtn = UIButton(title: "确认修改", name: "button_orange",radius: 5)
        saveBtn.frame = CGRect(x: MARGIN, y: tempHeight + MARGIN30, width: SCREEN_WIDTH - MARGIN * 2, height: MARGIN50)
        addSubview(saveBtn)
        // 确认修改按钮 添加点击事件
        saveBtn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
    }
    
    @objc func clickItem() -> Void {
        let bean = CallbackBean(firstIn: holderViews[0].text, secondIn: holderViews[1].text)
        if let callback = complete {
            callback(bean)
        }
    }
}

extension ModifyPwdView {
    
    private func init_views() -> Void {
        (0..<titles.count).forEach { (i) in
            let input = TextFieldOffsetPlaceHolder()
            input.placeholder = placeholders[i]
            input.textColor = UIColor.lightGray
            input.keyboardType = .asciiCapable
            input.font = UIFont.systemFont(ofSize: 13)
            input.setValue(MARGIN * 5, forKey: "paddingLeft")
            holderViews.append(input)
            addSubview(input)
        }
    }
}
